package de.steric.battlemon.rendering;

import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER;
import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER;
import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW;
import static org.lwjgl.opengl.GL15.glBindBuffer;
import static org.lwjgl.opengl.GL15.glBufferData;
import static org.lwjgl.opengl.GL15.glDeleteBuffers;
import static org.lwjgl.opengl.GL15.glGenBuffers;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.text.ParseException;
import java.util.ArrayList;

import org.lwjgl.BufferUtils;

public class Mesh {
	float[][] vertexList;
	int[][] indexList;// index
	String matLib, name, matName;
	int vertexBufferID, indexBufferID;
	FloatBuffer vertexBuffer;
	IntBuffer indexBuffer;
	public Mesh() {
	}
	public void create()
	{
		vertexBufferID = glGenBuffers();
		vertexBuffer = BufferUtils.createFloatBuffer(vertexList.length * vertexList[0].length);
		for (int i = 0; i < vertexList.length; i++) {
			vertexBuffer.put(vertexList[i]);
		}
		vertexBuffer.flip();
		glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID);
		glBufferData(GL_ARRAY_BUFFER, vertexBuffer, GL_STATIC_DRAW);
		glBindBuffer(GL_ARRAY_BUFFER, 0);//unbind vertexbuffer
		indexBufferID = glGenBuffers();
		indexBuffer = BufferUtils.createIntBuffer(indexList.length * indexList[0].length);
		for (int i = 0; i < indexList.length; i++) {
			indexBuffer.put(indexList[i]);
		}
		indexBuffer.flip();
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferID);
		glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexBuffer, GL_STATIC_DRAW);
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);//unbind indexbuffer
	}

	public void delete() {
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
		glBindBuffer(GL_ARRAY_BUFFER, 0);
		glDeleteBuffers(vertexBufferID);
		glDeleteBuffers(indexBufferID);
		
	}
}
